<?php

$this->addTitle($lang['admin']['base_title']." - ".$lang['admin']['settings_title']);
echo "<h3>".$lang['admin']['settings_title']."</h3><br />";
echo "\n";

$data_saved=false;

//Daten speichern
echo "<pre>";
//print_r($_POST);


if(isset($_POST['settings_save'])){
	foreach($_POST as $key=>$value){
		$substr=explode("_",$key);
		
		if(isset($substr[1]) && is_numeric($substr[1]))
			$data_save[$substr[1]][$substr[0]]=$value;
	}
	
	foreach($data_save as $value){
		if($value['file']==1 && is_file(getcwd().'/'.$value['fileentry'].".settings.php")){
			$settings_datei = file($value['fileentry'].".settings.php");
			$write='';
			foreach($settings_datei as $zeile) {
				$zeile=str_replace(" ", "", $zeile);
				$part=explode(']',$zeile);
				switch($part[0]){
					case '$plugin[\'access_view_group\'':
						$write.='$plugin[\'access_view_group\'] = array(';
						if(isset($value['gaccess'])) $write.=list_array($value['gaccess']);
						$write.=");\n";
						break;
					case '$plugin[\'access_view_users\'':
						$write.='$plugin[\'access_view_users\'] = array(';
						if(isset($value['uaccess'])) $write.=list_array($value['uaccess']);
						$write.=");\n";
						break;
					case '$plugin[\'access_edit_group\'':
						$write.='$plugin[\'access_edit_group\'] = array(';
						if(isset($value['gedit'])) $write.=list_array($value['gedit']);
						$write.=");\n";
						break;
					case '$plugin[\'access_edit_users\'':
						$write.='$plugin[\'access_edit_users\'] = array(';
						if(isset($value['uedit'])) $write.=list_array($value['uedit']);
						$write.=");\n";
						break;
					default:
						$write.="$zeile";
				}
				
			}		
			$datei = fopen(getcwd().'/'.$value['fileentry'].".settings.php","w");
			fwrite($datei,  stripslashes($write));
			fclose($datei);	
		}
	}
}



$this->sql->query("SELECT \n\t* \nFROM \n\t".$this->sql->getpre()."users");
$list_user="";
while($this->sql->fetch_assoc()){
	$list_user.="<option value=\"".$this->sql->row['user_id']."\">".$this->sql->row['user_name']."</option>\n";

};
$this->sql->query("SELECT \n\t* \nFROM \n\t`".$this->sql->getpre()."group`");
$list_group="";
while($this->sql->fetch_assoc()){
	$list_group.="<option value=\"".$this->sql->row['group_key']."\">".$this->sql->row['group_title']."</option>\n";

};

function list_array($array){
	$return="";
	if(is_array($array)){
		foreach($array as $value){
			if($return=="")
				$return="".$value;
			else
				$return.=", ".$value;
		}
	}
	return $return;
}

function options_list($options, $array){
	foreach($array as $value)
		$options=str_replace("value=\"".$value."\"", "value=\"".$value."\" selected=\"selected\"", $options);
		
	return $options;
}
echo "</pre>";
?>
<script language="javascript">
update=0;

function change(file_id){
	document.getElementById('data_changed').style.display="";
	document.getElementById('data_saved').style.display="none";
	document.getElementById(file_id).value=1;
	update=1;
}
</script>
<form id="form_0" name="form1" method="post" action="<?php echo fullURL(); ?>">
<div align="center">
	<div id="data_changed" style="display:none; font-weight:bold; font-size:12px; color:#000000; width:50%; text-align:center; padding:4px; margin-bottom:8px; background:#E14517; border-style:solid; border-width:3px; border-color:#990000; filter:alpha(opacity=60); -moz-opacity: 0.6; opacity: 0.6;"><?php echo $lang['admin']['info_nsave']; ?></div>
	<div id="data_saved" style=" <?php if(isset($data_saved) && !$data_saved) { ?>display:none;<?php }; ?> font-weight:bold; font-size:12px; color:#000000; width:50%; text-align:center; padding:4px; margin-bottom:8px; background:#009900; border-style:solid; border-width:3px; border-color:#003300; filter:alpha(opacity=60); -moz-opacity: 0.6; opacity: 0.6;"><?php echo $lang['admin']['info_saved']; ?></div>
</div>
<p><span style="font-size:80%;"><?php echo $lang['admin']['settings_info']; ?></span></p>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr>
    <th width="25" align="center">&nbsp;</th>
    <th width="180"><strong>Name</strong></th>
    <th><strong>Zugriff (Gruppen) </strong></th>
    <th><strong>Zugriff (Benutzer) </strong></th>
    <th><strong>Bearbeiten (User) </strong></th>
    <th><strong>Bearbeiten (User) </strong></th>
    <th>&nbsp;</th>
  </tr>
<?php 
$c=0;
foreach($plugin as $key=>$value){
if(isset($value[1]) && is_file(getcwd().'/'.$value[1].".settings.php")){
require(getcwd().'/'.$value[1].".settings.php");
if(is_numeric($key)){
?>
  <tr id="id_<?php echo $c; ?>_0" style=" <?php if($c%2){ ?>background-image:url(images/list_gray.png); filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/list_gray.png', sizingMethod='scale');<?php }; ?>">
    <td width="25" align="center"><strong><?php echo $c+1; ?></strong></td>
    <td width="180"><span title="<?php echo $value[1]; ?>"><?php echo $value[0]; ?></span></td>
    <td><?php echo list_array($plugin['access_view_group']); ?></td>
    <td><?php echo list_array($plugin['access_view_users']); ?></td>
    <td><?php echo list_array($plugin['access_edit_group']); ?></td>
    <td><?php echo list_array($plugin['access_edit_users']); ?></td>
    <td><a onclick="hide('id_<?php echo $c; ?>_0');hide('id_<?php echo $c; ?>_1');"><img src="images/icon_edit.png" alt="<?php echo $lang['admin']['link_edit']; ?>" title="<?php echo $lang['admin']['link_edit']; ?>" width="16" height="16" /></a></td>
  </tr>
  <tr id="id_<?php echo $c; ?>_1" style="display:none; <?php if($c%2){ ?>background-image:url(images/list_gray.png); filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/list_gray.png', sizingMethod='scale');<?php }; ?>">
    <td width="25" align="center"><strong><?php echo $c+1; ?></strong></td>
    <td width="180"><?php echo $value[0]; ?>
      <input type="hidden" name="file_<?php echo $c; ?>" id="file_<?php echo $c; ?>" value="0" />
      <input type="hidden" name="fileentry_<?php echo $c; ?>" value="<?php echo $value[1]; ?>" /></td>
    <td><select name="gaccess_<?php echo $c; ?>[]" size="5" multiple="multiple" onchange="change('file_<?php echo $c; ?>')">
		<?php echo options_list($list_group, $plugin['access_view_group']); ?>
                    </select></td>
    <td><select name="uaccess_<?php echo $c; ?>[]" size="5" multiple="multiple" onchange="change('file_<?php echo $c; ?>')">
		<?php echo options_list($list_user, $plugin['access_view_users']); ?>
                    </select></td>
    <td><select name="gedit_<?php echo $c; ?>[]" size="5" multiple="multiple" onchange="change('file_<?php echo $c; ?>')">
		<?php echo options_list($list_group, $plugin['access_edit_group']); ?>
                    </select></td>
    <td><select name="uedit_<?php echo $c; ?>[]" size="5" multiple="multiple" onchange="change('file_<?php echo $c; ?>')">
		<?php echo options_list($list_user, $plugin['access_edit_users']); ?>
                    </select></td>
    <td><a onclick="hide('id_<?php echo $c; ?>_0');hide('id_<?php echo $c; ?>_1');"><img src="images/icon_ok.png" alt="<?php echo $lang['admin']['link_close']; ?>" title="<?php echo $lang['admin']['link_close']; ?>" width="16" height="16" /></a></td>
  </tr>
<?php 
	$c++;
};
};
};
?>
</table>
<br />
<br />
<input type="submit" name="settings_save" value="<?php echo $lang['admin']['button_save']; ?>" />
</form>